package com.cdgc.web.service.Impl;

import com.cdgc.web.mapper.UserTableMapper;
import com.cdgc.web.mapper.UserdataMapper;
import com.cdgc.web.pojo.UserTable;
import com.cdgc.web.pojo.Userdata;
import com.cdgc.web.pojo.UserdataExample;
import com.cdgc.web.service.UserService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class UserServiceImpl implements UserService {
    @Resource
    private UserdataMapper userdataMapper;

    @Resource
    private UserTableMapper userTableMapper;
    @Override
    public Userdata doLogin(String username, String password) {
        UserdataExample userdataExample = new UserdataExample();
        UserdataExample.Criteria criteria = userdataExample.createCriteria();
        criteria.andUsernameEqualTo(username);
        criteria.andPasswordEqualTo(password);
        System.out.println(username+password);

        List<Userdata> userdataList = userdataMapper.selectByExample((userdataExample));

        if(userdataList!=null&&userdataList.size()>0){//判断集合是否为空
            return userdataList.get(0);//返回当前用户
        }
        return null;//否则返回空
    }

    @Override
    public List<UserTable> queryUsers() {
        return userTableMapper.selectByExample(null);
    }

    @Override
    public void addUser(UserTable userTable) {
        userTableMapper.insert(userTable);
    }

    @Override
    public UserTable queryUserById(Integer id) {
        return userTableMapper.selectByPrimaryKey(id);
    }

    @Override
    public void updateUser(UserTable userTable) {
        userTableMapper.updateByPrimaryKey(userTable);
    }

    @Override
    public void deleteUserById(Integer id) {
        userTableMapper.deleteByPrimaryKey(id);
    }
}
